package com.td.module.farmer.controller.app.management;


import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.farmer.controller.admin.management.vo.*;
import com.td.module.farmer.convert.management.ManagementConvert;
import com.td.module.farmer.dal.dataobject.management.ManagementDO;
import com.td.module.farmer.service.management.ManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "app - 农户土地信息")
@RestController
@RequestMapping("/farmer/management")
@Validated
public class ManagementAppController {

    @Resource
    private ManagementService managementService;

    @PostMapping("/create")
    @ApiOperation("创建农户土地信息")
    public CommonResult<Long> createManagement(@Valid @RequestBody ManagementCreateReqVO createReqVO) {
        return success(managementService.createManagement(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新农户土地信息")
    public CommonResult<Boolean> updateManagement(@Valid @RequestBody ManagementUpdateReqVO updateReqVO) {
        managementService.updateManagement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除农户土地信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteManagement(@RequestParam("id") Long id) {
        managementService.deleteManagement(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得农户土地信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<ManagementRespVO> getManagement(@RequestParam("id") Long id) {
        return success(managementService.getManagement(id));
    }

    @GetMapping("/getFarmer")
    @ApiOperation("根据农户编码获得农户土地信息")
    public CommonResult<ManagementRespVO> getFarmerManagement(@RequestParam("farmerCode") String farmerCode) {
        ManagementDO management = managementService.getManagement(farmerCode);
        ManagementRespVO managementRespVO = ManagementConvert.INSTANCE.convert(management);
        return success(managementRespVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得农户土地信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<ManagementRespVO>> getManagementList(@RequestParam("ids") Collection<Long> ids) {
        List<ManagementDO> list = managementService.getManagementList(ids);
        return success(ManagementConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得农户土地信息分页")
    public CommonResult<PageResult<ManagementPageReqVO>> getManagementPage(@Valid ManagementPageReqVO pageVO) {
        PageResult<ManagementDO> pageResult = managementService.getManagementPage(pageVO);
        return success(ManagementConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出农户土地信息 Excel")
    @OperateLog(type = EXPORT)
    public void exportManagementExcel(@Valid ManagementExportReqVO exportReqVO,
                                      HttpServletResponse response) throws IOException {
        List<ManagementDO> list = managementService.getManagementList(exportReqVO);
        // 导出 Excel
        List<ManagementExcelVO> datas = ManagementConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "农户土地信息.xls", "数据", ManagementExcelVO.class, datas);
    }

    @GetMapping("/get-mobile")
    @ApiOperation("根据农户手机号获得农户土地信息")
    public CommonResult<ManagementRespVO> getMobileManagement(@RequestParam("mobile") String mobile) {

        return success(managementService.getMobileManagement(mobile));
    }

}
